
require('type')
local class = require('class')
local ascii = require('ascii')

-- 字体类
local Font = class()

-- 字体, 字号
function Font:init(fontName, fontSize)
	if isNil(fontSize) then 
		fontSize = fontName
		fontName = 'C:/Windows/Fonts/simsun.ttc'
	end
	if not isNumber(fontSize) then
		fontSize = 14
	end
	self._fontSize = fontSize
	self._text = ''
	self._text8 = ''
	self._width = 0
	self._font = font_create(fontName, fontSize, 4)
	self._x = 0
	self._y = 0
end

-- 设置文字
function Font:setString(text)
	if self._font == nullptr then return end
	if isNil(text) then
		self._text =  string.empty
		self._text8 = string.empty
		self._width = 0
	else
		self._text =  tostring(text)
		self._text8 = ascii(self._text)
		self._width = font_getWidth(self._font, self._text8)
	end
end


-- 取文字
function Font:getString(text)
	if self._font == nullptr then return string.empty end
	return self._text
end

-- 颜色
function Font:setColor(color)
	if self._font == nullptr then return end
	return font_setColor(self._font, isNumber(color) and color or 0xFFFFFFFF)
end

function Font:getColor()
	if self._font == nullptr then return 0xFFFFFFFF end
	return font_getColor(self._font)
end

function Font:setPosition(x, y)
    if self._font == nullptr then return end
    self._x = isNumber(x) and x or 0
	self._y = isNumber(y) and y or 0
end

function Font:setPositionX(x)
    if self._font == nullptr then return end
    self._x = isNumber(x) and x or 0
end

function Font:setPositionY(y)
    if self._font == nullptr then return end
    self._y = isNumber(y) and y or 0
end

function Font:getPosition()
    return self._x, self._y
end

function Font:getPositionX()
    return self._x
end

function Font:getPositionY()
    return self._y
end

-- 阴影
function Font:setShadow()
	if self._font == nullptr then return end
	return font_setShadow(self._font)
end

-- 宽高
function Font:getSize()
	if self._font == nullptr or self._width == 0 then return 0, 0 end
	return self._width, self._fontSize
end

function Font:getWidth()
	if self._font == nullptr then return 0 end
	return self._width
end

function Font:getHeight()
	if self._font == nullptr or self._width == 0 then return 0 end
	return self._fontSize
end

-- 渲染文字
function Font:render()
	if self._font == nullptr or not self._text8 then return end
	return font_render(self._font, self._x, self._y, self._text8)
end


-- 销毁
function Font:release()
    if self._font ~= nullptr then
        font_release(self._font)
        self._font = nullptr
    end
end



function Font:汉化()
	self.设文字 = self.setString
	self.取文字 = self.getString
	self.设颜色 = self.setColor
	self.取颜色 = self.getColor
    self.设坐标 = self.setPosition
    self.设坐标x = self.setPositionX
    self.设坐标y = self.setPositionY
    self.取坐标 = self.getPosition
    self.取坐标x = self.getPositionX
    self.取坐标y = self.getPositionY   
	self.设阴影 = self.setShadow
	self.取宽高 = self.getSize
	self.取宽度 = self.getWidth
	self.取高度 = self.getHeight
	self.渲染 = self.render
	self.销毁 = self.release
end




return Font